module adisplay
import StdEnv, japi

/* *********************************
This example shows how to create a new led display.
Led displays are completely useless. However, I added 
this feature because I think it is cute.
***************************************/

:: State= { frame::!Framer, start::!TDev, 
            tens::!D, secs::!D,
            ticks::!Int, chrono::!Bool}

Start world
  # (js, world) = begin_japi world
    (frm, js) = jnewFrame FLOW  "Led counter" js
    (frm, js) = jverticalLayout frm js
    (frm, js) = vGap frm 60 js
    
    (pad, js)= jnewPanel FLOW frm js
    
    (panel, js)= jnewPanel FLOW frm js
    (panel, js)= jhorizontalLayout panel js
    (panel, js)= hGap panel 20 js
    
    (t, js) = jnew7seg  (WH 30 40) 0 J_RED panel js
    (u, js) = jnew7seg  (WH 30 40) 0 J_RED panel js
    
    frm= bg_color black frm 
    (mp, js) = jnewButton  FLOW "Start" frm js
    mp= bg_color yellow mp
    (frm, js)= jpack frm js
    (frm, js) = jshow frm js
    s= { frame= frm, start= mp,
         tens= t, secs= u,
         ticks= 0, chrono= False}
    js= loop s js
  = end_japi js world
where
    loop :: !State !JapiState -> JapiState
    loop s js
       # (x, js) = jsleep 500 js
       # (na, js) = jgetaction x js
       | na== (act s.frame) = jquit js
       | na== (act s.start) && s.chrono
           # (t, js)= jsetval s.tens 0 js
             stt= jsetText s.start "Start" 
             (u, js) = jsetval s.secs 0 js
           = loop { s&chrono= False, ticks= 0, 
                    tens= t, secs=u, start= stt} js
       | na== (act s.start) && (not s.chrono)
           # stp = jsetText s.start "Stop"
           = loop {s&chrono= True, start= stp} js
       | s.chrono
           # v1= (s.ticks+1) rem 60
           # (t, js)= jsetval s.tens (v1/10) js
             (u, js) = jsetval s.secs (v1 rem 10) js
           = loop {s& secs= u, tens=t, ticks= v1} js
       | otherwise = loop s js
    
 